<?php
$loader->requireOnce('includes/AbsReportTemplate.class.php');
$loader->requireOnce('includes/report03/InsuranceReportModel.class.php');

/**
 * Daily revenue
 * @author ptnguyen
 *
 */
class Report03 extends AbsReportTemplate {
    private $reportModel;
    private $total;
    
    function __construct($name) {
        parent::__construct($name);
    }
    
    /**
     * (non-PHPdoc)
     * @see modules/report/local/includes/AbsReportTemplate#buildReportModel()
     */
    function buildReportModel() {
        $examType =   EnumerationConstant::EXAMINATION_TYPE_INSURANCE_KEY;
        $start  =   $this->startDate . " 00:00";
        $end    =   $this->endDate . " 23:59";
        $sql    =   array(
            'cols'  =>  "pp.ticket_id, pr.patient_code, pe.name patient_name, pp.original_price, pp.insurance_price,
                        pp.patient_price, SUM(pr.amount) real_payment",
            'from'  =>  "pay_receipt pr
                        INNER JOIN pay_payment pp ON (pp.ticket_id = pr.ticket_id)
                        INNER JOIN person pe ON (pr.patient_id = pe.person_id)",
            'where' =>  "pr.created_time >= '$start'
                AND pr.created_time <= '$end'
                AND pp.enum_examination_type = '$examType'",
            'groupby'   =>  "pp.ticket_id"
        );
        $ds =   new Datasource_sql();
        $ds->setup($this->_db, $sql);
        $res    =   $ds->toArray();
        $reportModel    =   new InsuranceReportModel();
        $total  =   array(
            'original_price'    =>  0,
            'insurance_price'   =>  0,
            'patient_price'     =>  0,
            'real_payment'      =>  0
        );
        foreach ($res as &$item) {
            $objReportItem    =   new InsuranceReportItem($item['ticket_id'], $item['patient_code'], $item['patient_name'], $item['original_price'],
                $item['insurance_price'], $item['patient_price'], $item['real_payment']);
            $reportModel->addItem($objReportItem);
            $total['original_price']    +=  $item['original_price'];
            $total['insurance_price']   +=  $item['insurance_price'];
            $total['patient_price']     +=  $item['patient_price'];
            $total['real_payment']      +=  $item['real_payment'];
        }
        $this->reportModel  =   $reportModel;
        // Format total prices
        foreach ($total as &$item) {
        	$item  =   L10nHelper::renderNumber($item);
        }
        $this->total        =   $total;
    }
    
    /**
     * (non-PHPdoc)
     * @see modules/report/local/includes/AbsReportTemplate#renderReportHtml()
     */
    function renderReportHtml() {
        $this->_controller->assign('name', $this->name);
        $this->_controller->assign('date', $this->getDate());
        $this->_controller->assign('index', 1);
        $this->_controller->assign('reportModel', $this->reportModel);
        $this->_controller->assign('total', $this->total);
        $this->_controller->assign('itemNumber', count($this->reportModel->getItemList()));
        if ($this->_print) {
            return $this->_controller->view->render('print.html');
        }
        return $this->_controller->view->render('view.html');
    }
}